package com.dys.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * @className: com.dys.servlet.MyServlet01
 * @description: com.dys.servlet
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/18 20:40
 */
public class MyServlet01 extends HttpServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        // 动态生成数据
        int num = new Random().nextInt();
        String message = num % 2 == 0 ? "偶数" : "奇数";
        String tag = "<h2>" + message + "</h2>";
        // 浏览器做出响应
        res.setContentType("text/html");
        res.setCharacterEncoding("utf-8");
        PrintWriter writer = res.getWriter();
        // 该打印流指向了浏览器
        writer.write(tag);
    }
}
